package com.hopedove.view;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentActivity;

public class TipsDialogFragment extends DialogFragment {

    private View view;

    public static TipsDialogFragment newInstance(View v) {
        TipsDialogFragment fragment = new TipsDialogFragment();
        fragment.view = v;
        return fragment;
    }

    public static TipsDialogFragment showNewInstance(FragmentActivity activity, View v, boolean cancelable) {
        TipsDialogFragment dialog = TipsDialogFragment.newInstance(v);
        dialog.setCancelable(cancelable);
        dialog.show(activity.getSupportFragmentManager(), "");
        return dialog;
    }

    public static TipsDialogFragment showNewInstance(FragmentActivity activity, @LayoutRes int layoutId, boolean cancelable) {
        View v = activity.getLayoutInflater().inflate(layoutId, null);
        return showNewInstance(activity, v, cancelable);
    }

    public static void dismissTipDialog(TipsDialogFragment dialog) {
        if (dialog != null && dialog.getDialog() != null && dialog.getDialog().isShowing()) {
            dialog.dismissAllowingStateLoss();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);
    }

    @Override
    public void onStart() {
        // 设置自定义的view
        if (view != null && getDialog() != null) {
            getDialog().setContentView(view);
        }
        // 背景设为透明
        if (getDialog() != null && getDialog().getWindow() != null) {
            getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        super.onStart();
    }
}
